﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using Sio.Mdm.Client;
using Sio.Mdm.Types;
using IDM.BF.AdministrationService;
using Sio.Mdm.Entities;

namespace IDM.Web
{
    public class Global : System.Web.HttpApplication
    {   
        protected void Application_Start(object sender, EventArgs e)
        {            
            Sio.Mdm.Server.Server.Initialize();
            //IDM.Server.Server.Interceptors.Add(new Base.TokenWriter());

            Base.SessionProvider provider = new Base.SessionProvider();
            
            ServiceFactory.Interceptors.Add(provider);
            SessionFactory.RegisterProvider(provider);

            QueryGeneratorManager.SetGenerator(new FirebirdGenerator());
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {
            IAuthorizationService auth = ServiceFactory.GetService<IAuthorizationService>();
            Session session = Sio.Mdm.Types.Session.Current;
            
            auth.Logout(ref session);
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Sio.Mdm.Server.Server.Destroy();
        }
    }
}